package com.liang.paul.officelite.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.amulyakhare.textdrawable.TextDrawable;
import com.amulyakhare.textdrawable.util.ColorGenerator;
import com.liang.paul.officelite.R;
import com.liang.paul.officelite.beams.TeamBeam;
import com.liang.paul.officelite.holders.TeamListHolder;

import java.util.ArrayList;


/**
 * Created by paulliang on 16/5/1.
 */
public class TeamListAdapter extends BaseAdapter {

    Context context;
    ArrayList<TeamBeam> datas;

    public TeamListAdapter(Context context, ArrayList<TeamBeam> datas){
        this. context = context;
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TeamListHolder holder;
        if(convertView != null){
            holder = (TeamListHolder) convertView.getTag();
        }else {
            convertView = LayoutInflater.from(context).inflate(R.layout.list_team, null);
            holder = new TeamListHolder(context,
                    (ImageView) convertView.findViewById(R.id.image_teamlist_head),
                    (TextView)convertView.findViewById(R.id.txt_teamlist_name),
                    (TextView)convertView.findViewById(R.id.txt_teamlist_createtime));
            convertView.setTag(holder);
        }
        ColorGenerator generator = ColorGenerator.MATERIAL;
        int color = generator.getColor(datas.get(position).getName());
        TextDrawable drawable = TextDrawable.builder()
                .beginConfig()
                    .withBorder(4)
                .endConfig()
                .buildRound(datas.get(position).getName().substring(0, 1), color);
        holder.setImage(drawable);
        holder.setName(datas.get(position).getName());
        holder.setCreateDate(datas.get(position).getCreatedAt());
        return convertView;
    }
}
